package com.lazyfish.bootcm.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lazyfish.bootcm.entity.Customer;
import com.lazyfish.bootcm.entity.User;
import com.lazyfish.bootcm.service.CustomerService;
import com.lazyfish.bootcm.util.ResultBuild;
@RestController
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @RequestMapping(value = "/login")
    public ResultBuild login(User user, HttpSession session){
        if(user.getUsername().equals("xiaoxi") && user.getPassword().equals("xiaoxi233")){
            //将用户对象添加到Session中
            session.setAttribute("USER_SESSION",user);
            session.setMaxInactiveInterval(30 * 60);
            return new ResultBuild(200,true);
        }
        return new ResultBuild(200,false);
    }
    
    @RequestMapping("/getStatistics")
    public ResultBuild getStatistics() {
    	return new ResultBuild(200, customerService.getStatistics());
    }

    @RequestMapping("/getCustomerList")
    public ResultBuild getCustomerListByselect(
    		@RequestParam(required = false) String source,
    		@RequestParam(required = false) String industry,
    		@RequestParam(required = false) Integer level,
    		@RequestParam(required = false) String pattern,
    		@RequestParam(required = false) Integer pageNum,
    		@RequestParam(required = false) Integer pageLen){
        return new ResultBuild(200,customerService.getCustmerListBySelect(source, industry,
        		level, pattern, pageNum, pageLen));
    }
    
    @RequestMapping("/deleteCustomer")
    public ResultBuild deleteCustomer(int... id){
    	int[] result = new int[id.length];
    	for (int i = 0; i < id.length; i++) {
			result[i] = customerService.deleteCustomer(id[i]);
		}
        return new ResultBuild(200, result);
    }
    
    @RequestMapping("/AddCustomer")
    public ResultBuild AddCustomer(Customer customer){
        return new ResultBuild(200,customerService.AddCustomer(customer));
    }
    @RequestMapping("/ModifyCustomer")
    public ResultBuild ModifyCustomer(Customer customer){
        return new ResultBuild(200,customerService.ModifyCustomer(customer));
    }
}
